unit clsLocalCobranca;

interface

uses Dm, dbtables, biblio, sysutils;

type
  TLocalCobranca = class(TObject)
  private
    Local_Codigo: string;
  public
    Cid_Codigo: string;
    TipoL_Codigo: string;
    Local_Descricao: string;
    Local_Endereco: string;
    Local_Cep: string;
    Local_Fone: string;
    Local_Somar: string; //0-Nao 1-Sim
    ID_Taxa: Integer;
    Taxa_Publicidade: Double;
    Conta_codigo: string[3];

    Cod_Local: string;
    function Gravar(p_Local_Codigo: string): Byte;
    function Recuperar(p_Local_Codigo: string): Byte;
    function RecuperarNome(p_Local_Nome: string): Byte;
    function Recuperar_ultimo: string;
    function Excluir(p_Local_Codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_nome: string);
  end;

implementation

function TLocalCobranca.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM LocalCobranca where Local_Codigo=:k_Local_Codigo');
  tq.parambyname('k_Local_Codigo').asstring := strzero(p_Local_Codigo, 3);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO LocalCobranca VALUES(' +
      ':k_Local_Codigo,' +
      ':k_cid_codigo,' +
      ':k_Tipol_Codigo,' +
      ':k_Local_Descricao,' +
      ':K_Local_endereco,' +
      ':k_local_cep,' +
      ':k_local_fone,' +
      ':k_Local_Somar, ');
    if (ID_Taxa <> 0) and (ID_Taxa <> -1) then
      tq.SQL.Add(':k_ID_Taxa,')
    else
      tq.SQL.Add('null,');
    tq.SQL.Add(':k_Taxa_Publicidade, ');
    if Trim(Conta_codigo) <> '' then
      tq.SQL.Add(':k_Conta_Codigo ')
    else
      tq.SQL.Add('null ');
    tq.SQL.Add(')');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE LocalCobranca SET Local_Descricao=:k_Local_Descricao,' +
      'Cid_Codigo=:k_cid_codigo,' +
      'TipoL_Codigo=:k_tipol_codigo,' +
      'local_endereco=:k_local_Endereco,' +
      'local_cep=:k_local_cep,' +
      'local_fone=:k_local_fone,' +
      'Local_Somar=:k_Local_Somar, ');
    if (ID_Taxa <> 0) and (ID_Taxa <> -1) then
      tq.SQL.Add('ID_Taxa =:k_ID_Taxa, ')
    else
      tq.SQL.Add('ID_Taxa = null,  ');
    tq.SQL.Add('Taxa_Publicidade =:k_Taxa_Publicidade, ');
    if Trim(Conta_codigo) <> '' then
      tq.SQL.Add('Conta_Codigo =:k_Conta_Codigo ')
    else
      tq.SQL.Add('Conta_codigo = null ');
    tq.SQL.Add('WHERE Local_Codigo=:k_Local_Codigo');
  end;
  tq.parambyname('k_Local_Codigo').asstring := strzero(p_Local_Codigo, 3);
  tq.parambyname('k_Local_Descricao').asstring := Local_Descricao;
  tq.parambyname('k_Cid_Codigo').asstring := strzero(cid_codigo, 4);
  tq.parambyname('k_Tipol_Codigo').asstring := strzero(tipol_codigo, 2);
  tq.parambyname('k_local_endereco').asstring := local_endereco;
  tq.parambyname('k_local_cep').asstring := local_cep;
  tq.parambyname('k_local_fone').asstring := local_fone;
  tq.parambyname('k_Local_Somar').AsString := Local_Somar;
  if (ID_Taxa <> 0) and (ID_Taxa <> -1) then
    tq.ParamByName('k_ID_Taxa').AsInteger := ID_Taxa;
  tq.ParamByName('k_Taxa_Publicidade').AsFloat := Taxa_Publicidade;
  if trim(Conta_codigo) <> '' then
    tq.ParamByName('k_Conta_codigo').AsString := Conta_codigo;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TLocalCobranca.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM LocalCobranca where Local_Codigo=:k_Local_Codigo');
  tq.Params[0].asstring := strzero(p_Local_Codigo, 3);
  tq.open;
  Local_Codigo := p_Local_Codigo;
  if tq.eof then begin
    Local_Descricao := '';
    cid_codigo := '';
    tipol_codigo := '';
    local_endereco := '';
    local_cep := '';
    local_fone := '';
    Local_Somar := '1';
    ID_Taxa := -1;
    Taxa_Publicidade := 0;
    Conta_Codigo := '';
    Result := 0;
  end else begin
    Local_Descricao := tq.fieldbyname('Local_Descricao').asstring;
    cid_codigo := tq.fieldbyname('cid_codigo').asstring;
    tipol_codigo := tq.fieldbyname('tipol_codigo').asstring;
    local_endereco := tq.fieldbyname('local_endereco').asstring;
    local_cep := tq.fieldbyname('local_cep').asstring;
    local_fone := tq.fieldbyname('local_fone').asstring;
    Local_Somar := tq.fieldbyname('Local_Somar').asstring;
    ID_Taxa := StrToIntDef(tq.FieldByName('ID_Taxa').AsString, -1);
    Taxa_Publicidade := tq.FieldByName('Taxa_Publicidade').AsFloat;
    Conta_codigo := tq.FieldByName('Conta_codigo').AsString;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TLocalCobranca.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM LocalCobranca WHERE Local_Codigo=:k_Local_Codigo');
  tq.parambyname('k_Local_Codigo').asstring := strzero(p_Local_Codigo, 3);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TLocalCobranca.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from LocalCobranca where Local_Descricao>=:k_Local_Descricao ');
  //  if p_nome=' ' then p_q.sql.add('or Local_Descricao=null');
  p_q.sql.add('order by Local_Descricao');
  p_q.parambyname('k_Local_Descricao').asstring := p_nome;
end;

function TLocalCobranca.Recuperar_ultimo: string;
var
  tq: TQuery;
begin
  tq := TQuery.create(DmInt.Db);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.sql.clear;
  tq.Params.clear;
  tq.SQL.add('Select max(local_codigo) as maior from localcobranca');
  tq.open;
  if tq.fieldbyname('maior').AsString <> '' then
    Result := IntToStr(strtoint(tq.fieldbyname('maior').Asstring) + 1)
  else
    Result := '1';
  if Result = '1000' then
    Result := '999';
  tq.close;
  tq.free;
end;

function TLocalCobranca.RecuperarNome(p_Local_Nome: string): Byte;
var
  tq: TQuery;
  tam: Integer;
begin
  tam := length(p_Local_Nome);
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM LocalCobranca where substr(Local_Descricao,1,' + IntToStr(tam) + ') =:k_Local_Desc');
  tq.Params[0].asstring := p_Local_Nome;
  tq.open;
  Local_Descricao := p_Local_Nome;
  if tq.eof then begin
    cid_codigo := '';
    tipol_codigo := '';
    local_endereco := '';
    local_cep := '';
    local_fone := '';
    Local_Somar := '1';
    ID_Taxa := -1;
    Taxa_Publicidade := 0;
    Conta_codigo := '';
    Result := 0;
  end else begin
    Cod_Local := tq.fieldbyname('Local_Codigo').asstring;
    Local_Descricao := tq.fieldbyname('Local_Descricao').asstring;
    cid_codigo := tq.fieldbyname('cid_codigo').asstring;
    tipol_codigo := tq.fieldbyname('tipol_codigo').asstring;
    local_endereco := tq.fieldbyname('local_endereco').asstring;
    local_cep := tq.fieldbyname('local_cep').asstring;
    local_fone := tq.fieldbyname('local_fone').asstring;
    Local_Somar := tq.fieldbyname('Local_Somar').asstring;
    ID_Taxa := StrToIntDef(tq.FieldByName('ID_Taxa').AsString, -1);
    Taxa_Publicidade := tq.FieldByName('Taxa_Publicidade').AsFloat;
    Conta_codigo := tq.FieldByName('Conta_codigo').AsString;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

end.

